package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.base.Constant;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/1.
 */

public class WelcomeGuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.vp_show)
    ViewPager vpShow;
    @BindView(R.id.iv_indicator_1)
    ImageView ivIndicator1;
    @BindView(R.id.iv_indicator_2)
    ImageView ivIndicator2;
    @BindView(R.id.iv_indicator_3)
    ImageView ivIndicator3;
    @BindView(R.id.tv_open)
    TextView tvOpen;
    @BindView(R.id.tv_skip)
    TextView tvSkip;

    private PagerAdapter adapter;
    private List<Drawable> list;

    @Override
    public void initData(Bundle savedInstanceState) {
        list = new ArrayList<>();
        list.add(getResources().getDrawable(R.mipmap.guide1));
        list.add(getResources().getDrawable(R.mipmap.guide2));
        list.add(getResources().getDrawable(R.mipmap.guide3));
        adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return list != null ? list.size() : 0;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(App.getInstance());
                imageView.setImageDrawable(list.get(position));
                imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

        };
        vpShow.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(tvSkip)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.FIRST_OPEN, false);
                        Router.newIntent((Activity) App.getInstance()).to(LoginActivity.class).data(new Bundle()).launch();
                        finish();
                    }
                });
        RxView.clicks(tvOpen)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.FIRST_OPEN, false);
                        Router.newIntent((Activity) App.getInstance()).to(LoginActivity.class).data(new Bundle()).launch();
                        finish();
                    }
                });
        vpShow.setOnPageChangeListener(this);

    }

    @Override
    protected void initPermission() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    protected void setScreen() {
        super.setScreen();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags = (WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(lp);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 2) {
            tvSkip.setVisibility(View.GONE);
            tvOpen.setVisibility(View.VISIBLE);
        } else {
            tvSkip.setVisibility(View.VISIBLE);
            tvOpen.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
